import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class DSliverList extends SliverList {
  DSliverList({required NullableIndexedWidgetBuilder builder, int childCount=0})
      : super(
            delegate:
                SliverChildBuilderDelegate(builder, childCount: childCount));
}

class DSliverGrid extends SliverGrid {
  DSliverGrid(
      NullableIndexedWidgetBuilder builder, {
    int childCount = 0,
    int crossAxisCount = 2,
    double mainAxisSpacing = 0,
    double crossAxisSpacing = 0,
        double childAspectRatio=1.0
  }) : super(
            delegate:
                SliverChildBuilderDelegate(builder, childCount: childCount),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: crossAxisCount,
                mainAxisSpacing: mainAxisSpacing,
                crossAxisSpacing: crossAxisSpacing,childAspectRatio:childAspectRatio ));
}


class AnimatedTransform extends StatefulWidget {
  final Widget child;
  final Duration duration;
  final Curve curve;
  final Offset offset;

  AnimatedTransform(
      {required this.child, this.offset = Offset.zero,required this.duration, required this.curve});

  @override
  _AnimatedTransformState createState() => _AnimatedTransformState();
}

class _AnimatedTransformState extends State<AnimatedTransform>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Offset offset;

  @override
  void initState() {
    super.initState();
    offset = widget.offset;
    _controller = AnimationController(upperBound: double.infinity,vsync: this);
    _controller.addListener(onAnimListener);
  }

  @override
  void didUpdateWidget(covariant AnimatedTransform oldWidget) {
    super.didUpdateWidget(oldWidget);
    if(oldWidget.offset.dy==widget.offset.dy){
      return;
    }
    _controller.stop();
    _controller.animateTo(widget.offset.dy,duration:widget.duration,curve:widget.curve);
  }

  void onAnimListener() {
    offset=Offset(0,_controller.value);
    setState(() {

    });
  }

  @override
  Widget build(BuildContext context) {
    return Transform.translate(
      offset: offset,
      child: widget.child,
    );
  }
}

